package com.yqyd.web.controller.hy.app;

import com.yqyd.common.annotation.Anonymous;
import com.yqyd.common.core.domain.R;
import com.yqyd.hy.domain.TpVote;
import com.yqyd.hy.domain.TpVoteRecord;
import com.yqyd.hy.dto.AddVoteDto;
import com.yqyd.hy.service.ITpVoteRecordService;
import com.yqyd.hy.service.ITpVoteService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分类
 */
@RestController
@RequestMapping("/api/app/vote")
@CrossOrigin
public class ApiVoteController {

    @Resource
    private ITpVoteService voteService;
    @Resource
    private ITpVoteRecordService recordService;


    @GetMapping("/List/{uuid}")
    @Anonymous
    public R<Map<String, Object>> List(@PathVariable String uuid) {
        Map<String, Object> map = new HashMap<>();
        map.put("UUID", uuid);
        map.put("Record", recordService.selectTpVoteRecordByUuid(uuid));
        map.put("List", voteService.selectTpVoteList(new TpVote()));
        return R.ok(map);
    }

    /**
     * 投票
     * @param dto
     * @return
     */
    @PostMapping("/Add")
    @Anonymous
    public R<String> Add(@RequestBody @Valid AddVoteDto dto) {
        int row = recordService.insertTpVoteRecord(
                new TpVoteRecord().setVoteId(dto.getVoteId()
                        ).setUserId(dto.getUuid())
        );
        return row > 0? R.ok() : R.fail("投票失败：您已经投过票了");
    }

}
